//
//  JWButton.m
//  见闻新闻
//
//  Created by brother on 16/3/2.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWButton.h"

#import "JWFeaturedController.h"

#define screenWidth  [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
#define AnimateWithDuration 2
//按钮的间距
#define delta 90

@interface JWButton ()

@property (nonatomic ,strong)NSMutableArray *itmes;

@property (weak, nonatomic) UIButton *mianBtn;

@property (nonatomic ,assign)NSInteger btnTag;

@property (nonatomic ,strong)NSMutableArray *array;
@end

@implementation JWButton



- (NSMutableArray *)itmes {
    if (_itmes == nil) {
        _itmes = [NSMutableArray array];
    }
    return _itmes;
}
+ (instancetype)buttonView {
    return [[[NSBundle mainBundle] loadNibNamed:@"JWButton" owner:nil options:nil] lastObject];
}
//创建出按钮
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self initItems];
    }
    return self;
}


- (void)initItems {
    
    [self addBtnBgImage:@"1" tag:1000];
    
    [self addBtnBgImage:@"2" tag:1001];
    
    [self addBtnBgImage:@"3" tag:1002];
    
    [self addBtnBgImage:@"4" tag:1003];
    
}


- (void)addBtnBgImage:(NSString *)imageName tag:(NSInteger)tag {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    btn.tag = tag;

    [btn addTarget:self action:@selector(controllerLoadData:) forControlEvents: UIControlEventTouchUpInside];
    
    [self.itmes addObject:btn];
    [self addSubview:btn];
    
    
}


- (void)controllerLoadData:(UIButton *)btn {
        if (btn.tag == 1000) {
            if ([self.delegate respondsToSelector:@selector(apppkey:)]) {
                [self.delegate apppkey:@"jw10000001"];
            };
        }else if (btn.tag == 1001) {
            if ([self.delegate respondsToSelector:@selector(apppkey:)]) {
                [self.delegate apppkey:@"jw10000002"];
            };
        }else if (btn.tag == 1002) {
            if ([self.delegate respondsToSelector:@selector(apppkey:)]) {
                [self.delegate apppkey:@"jw10000003"];
            };
        }else if (btn.tag == 1003) {
            if ([self.delegate respondsToSelector:@selector(apppkey:)]) {
                [self.delegate apppkey:@"jw10000004"];
            };
        }else {
            return;
        }
    
    
}



- (void)showItmes:(BOOL)show {
    //2 实现 items 的显示位置
    for (UIButton *btn  in self.itmes) {
        // 显示动画效果
        // 创建主动画
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.duration = AnimateWithDuration;
        
        //平移动画
        CAKeyframeAnimation *positionAni = [CAKeyframeAnimation animation];
        positionAni.keyPath = @"position";
        // 注意  按钮的中心点和按钮的图层是一样的
        //添加一个旋转动画
        CAKeyframeAnimation *rotationAni = [CAKeyframeAnimation animation];
        rotationAni.keyPath = @"transform.rotation";
        //重新计算每个按钮x的值
        
            CGFloat btnCenterX = btn.center.x-10;
            CGFloat btnCenterY = btn.frame.size.height * 0.5 + ((screenHeight/2) / 4) * (btn.tag % 1000);
            //self.mianBtn.center.y + (btn.tag + 1) * (buttonViewH / 5);
            btn.center = CGPointMake(btnCenterX, btnCenterY);
            CGPoint showPosition = CGPointMake(btnCenterX, btnCenterY);
            //设置平移动画的路径
            //NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
            //NSValue *value1 = [NSValue valueWithCGPoint:btn.center];
            NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(btnCenterX, btnCenterY * 0.5)];
            NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(btnCenterX, btnCenterY * 1.1)];
            NSValue *value4 = [NSValue valueWithCGPoint:showPosition];
            if (show == YES) {
                //显示
                //设置平移动画的路径
                positionAni.values = @[value2,value3,value4];
                //设置旋转的路径
                rotationAni.values = @[@(M_PI * 2),@(M_PI * 4)];
                btn.center = showPosition;
                
            }else {
                //隐藏
                //设置平移动画的路径
                positionAni.values = @[value4,value3,value2];
                //设置旋转的路径
                rotationAni.values = @[@0,@(M_PI * 2),@0,@(-M_PI * 2)];
                btn.center = CGPointMake(btn.frame.size.width * 0.5, btn.frame.size.height * 0.5);
            }
        
        
        //添加子动画
        group.animations = @[positionAni,rotationAni];
        
        //执行主动画
        [btn.layer addAnimation:group forKey:nil];
    }
}




- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.frame = CGRectMake(7*(screenWidth/8),screenHeight*0.14,screenWidth * 0.2,screenHeight * 0.5);
    
    self.backgroundColor = [UIColor clearColor];
    
    self.alpha = 0;
    
    [self btns];
    
}

- (void)btns {
    //遍历按钮
    CGRect btnBounds = CGRectMake(0, 0 , self.frame.size.width *0.5 , 40);
    for (UIButton *btn  in self.itmes) {
        btn.bounds = btnBounds;
        //[self.array addObject:btn.tag];
        btn.center = CGPointMake(btn.frame.size.width *0.5 +10,btn.frame.size.height*0.5);
    }
    //把主要的按钮置顶
    //[self bringSubviewToFront:self.mianBtn];
}

@end
